package com.manjason.property.util;

import com.manjason.property.entity.Property;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class Util {

	public static Map<String, Property> propertyMap = new HashMap<>();

	public static Property getPropertyByPlayerName (String playerName) {
		return propertyMap.get(playerName);
	}

	public static void removeProperty (String playerName) {
		propertyMap.remove(playerName);
	}
	
	public static void sendMsg(CommandSender cs, String msg) {
		cs.sendMessage(msg);
	}

	public static String convertColor(String str) {
		return str.replace("&", "§");
	}
	
	public static String convertArgs (String[] args, int index) {
		StringBuilder sb = new StringBuilder();
		for (int i = index; i < args.length; i ++) {
			sb.append(args[i]);
			if (i != args.length - 1) {
				sb.append(" ");
			}
		}
		return sb.toString();
	}

	public static void sendFullMsg(String msg) {
		for (Player p : Bukkit.getOnlinePlayers())
			p.sendMessage(msg);
	}
	
	/**
	 * 格式=ID:数据(可空)-数量
	 * @param str
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ItemStack getItemStackByString (String str) {
		ItemStack item = null;
		String id = str.split("-")[0];
		int num = Integer.parseInt(str.split("-")[1]);
		if (id.split(":").length > 1) {
			item = new ItemStack(Integer.parseInt(id.split(":")[0]), num, Short.parseShort(id.split(":")[1]));
		} else {
			item = new ItemStack(Integer.parseInt(id), num);
		}
		return item;
	}
}
